package com.example.minemodule.viewmodel;

import android.app.Activity;
import android.app.Application;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;

import com.examination.mlib.constants.KitArouterConstants;
import com.example.minemodule.R;
import com.example.minemodule.Utils.MineUtils;
import com.example.minemodule.databinding.MineFragmentBinding;
import com.example.minemodule.databinding.RecordActivityDataBinding;
import com.example.minemodule.entity.BloodBean;
import com.example.minemodule.entity.RecordWeightEntity;
import com.example.minemodule.model.RecordModel;
import com.example.minemodule.ui.RecordWeightActivity;
import com.yilijk.base.base.BaseViewModel;
import com.yilijk.base.network.bean.Resource;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2021/11/12
 */
public class RecordViewModel extends BaseViewModel<RecordModel> {

    private RecordWeightActivity activity;
    private RecordActivityDataBinding binding;


    public RecordViewModel(@NonNull @NotNull Application application) {
        super(application);
    }


    @Override
    public void createRepository() {
        super.createRepository();
        repository = new RecordModel();
    }

    /**
     * 过去当前VM的activity binding
     */
    public void setBinding(RecordWeightActivity activity, RecordActivityDataBinding binding){
        this.activity = activity;
        this.binding = binding;
    }


    /**
     * 设置状态栏及UI
     */
    public void initUI(int pageType,int inquiringPatientId,int id){
        binding.titleBar.txt_title.setText(pageType == 0 ? R.string.record_title_height:R.string.record_title_blood);
        binding.titleBar.txt_title.setTypeface(Typeface.DEFAULT_BOLD);
        binding.setPageType(pageType);
        binding.setInquiringPatientId(inquiringPatientId);
        binding.setId(id);
    }

    public void initBlood(float blood,String times){
        if (blood == 0){
            binding.rulerBlood.setValue(4.0f, 0, 15, 0.1f);
            blood = 4.0f;
            binding.setBloodValue(blood+"");
        }else{
            binding.rulerBlood.setValue(blood, 0, 15, 0.1f);
            binding.setBloodValue(blood+"");
        }
        binding.setBloodTime(times);
    }

    public void initHeight(int height,float weight){
        if (height == 0){
            binding.rulerHeight.setValue(160, 10, 250, 1);
            height = 160;
            binding.setHeightValue(height+"");
        }else{
            binding.rulerHeight.setValue(height, 10, 300, 1);
            binding.setHeightValue(height+"");
        }
        if (weight == 0){
            weight = 55.0f;
            binding.rulerWeight.setValue(55.0f, 10, 250, 0.1f);
            binding.setWeightValue(weight+"");
        }else{
            binding.rulerWeight.setValue(weight, 1, 200, 0.1f);
            binding.setWeightValue(weight+"");
        }
    }

    public void saveBlood(int inquiringPatientId, String bloodFat, String bloodFatRecordTime, String bloodFatRecordSource){
        getBloodFatSave(inquiringPatientId,bloodFat,bloodFatRecordTime,bloodFatRecordSource)
                .observe(activity, new Observer<Resource<BloodBean.DataBean>>() {
                    @Override
                    public void onChanged(Resource<BloodBean.DataBean> dataBeanResource) {
                        if (dataBeanResource.data!=null){
                            // bloodPage: true, //是否是血脂页面
                            // level:0,  //0-偏低 1-正常 2-偏高
                            // booldValue:0,  //血脂值
                            // booldDesctibe:0,  //血脂描述文案
                            // updateTime:0,  //时间
                            // healthAdvice:0,  //健康建议
                            // healthAdviceList:0,  //健康建议数组<string>
                            Bundle bundle = new Bundle();
                            bundle.putInt("inquiringPatientId",inquiringPatientId);
                            bundle.putBoolean("bloodPage",true);
                            bundle.putInt("level",dataBeanResource.data.getBloodFatConclusionValue());
                            bundle.putString("booldValue", String.valueOf(dataBeanResource.data.getBloodFat()));
                            bundle.putString("booldDesctibe",dataBeanResource.data.getBloodFatName());
                            bundle.putString("updateTime",dataBeanResource.data.getBloodFatCreateTime());
                            bundle.putString("bloodFatReferenceBasis",dataBeanResource.data.getBloodFatReferenceBasis());
                            bundle.putString("healthAdvice","");
                            List<String> bloodFatHealthAdviceList = dataBeanResource.data.getBloodFatHealthAdviceList();
                            ArrayList<String> healthAdviceList = new ArrayList<>();
                            for (String s:bloodFatHealthAdviceList) {
                                healthAdviceList.add(s);
                            }
                            bundle.putStringArrayList("healthAdviceList",healthAdviceList);
                            List<String> bloodFatReferenceRangeList = dataBeanResource.data.getBloodFatReferenceRange();
                            ArrayList<String> bloodFatReferenceRangeAList = new ArrayList<>();
                            for (String s:bloodFatReferenceRangeList) {
                                bloodFatReferenceRangeAList.add(s);
                            }
                            bundle.putStringArrayList("bloodFatReferenceRange",bloodFatReferenceRangeAList);
                            MineUtils.push(activity, KitArouterConstants.MY_BUNDLE,"HealthBaseData",bundle);
                            activity.finish();
                        }
                    }
                });
    }

    public void saveHeight(){
        getRecordWeightEntity().observe(activity, new Observer<Resource<RecordWeightEntity.DataBean>>() {
            @Override
            public void onChanged(Resource<RecordWeightEntity.DataBean> dataBeanResource) {
                if (dataBeanResource.data!=null){
                    Bundle bundle = new Bundle();
                    bundle.putInt("inquiringPatientId",dataBeanResource.data.getInquiringPatientId());
                    bundle.putInt("height",dataBeanResource.data.getHeight());
                    bundle.putDouble("weight",dataBeanResource.data.getWeight());
                    bundle.putDouble("bmi",dataBeanResource.data.getBmi());
                    bundle.putString("healthAdvice",dataBeanResource.data.getHealthAdvice());
                    bundle.putStringArrayList("healthAdviceList",dataBeanResource.data.getHealthAdviceList());
                    bundle.putString("conclusion",dataBeanResource.data.getConclusion());
                    bundle.putString("updateTime",dataBeanResource.data.getUpdateTime());
                    MineUtils.push(activity, KitArouterConstants.MY_BUNDLE,"HealthBaseData",bundle);
                    activity.finish();
                }
            }
        });
    }


    /**
     * 保存
     */
    public LiveData<Resource<RecordWeightEntity.DataBean>> getRecordWeightEntity(){
        return getRepository().saveHeight(binding.getId(),binding.getInquiringPatientId(),binding.getHeightValue(),binding.getWeightValue());
    }

    /**
     * 保存
     */
    public LiveData<Resource<BloodBean.DataBean>> getBloodFatSave(int inquiringPatientId, String bloodFat, String bloodFatRecordTime, String bloodFatRecordSource){
        return getRepository().saveBloodFat(inquiringPatientId,bloodFat,bloodFatRecordTime,bloodFatRecordSource);
    }

}
